#include "driver/gpio.h"
#include "st7789.h"
#include <stdio.h>
#include <unistd.h>

void esp32c3_clk_pin(int val)
{
        gpio_set_level(2, val);
}

void esp32c3_mo_pin(int val)
{
        gpio_set_level(3, val);
}

void esp32c3_rst_pin(int val)
{
        gpio_set_level(10, val);
}

void esp32c3_cs_pin(int val)
{
        gpio_set_level(7, val);
}

void esp32c3_bl_pin(int val)
{
        gpio_set_level(11, val);
}

void esp32c3_msleep(int ms)
{
        usleep(1000 * ms);
}

void board_init()
{

        gpio_reset_pin(2);
        gpio_set_direction(2, GPIO_MODE_OUTPUT);
        gpio_reset_pin(3);
        gpio_set_direction(3, GPIO_MODE_OUTPUT);
        gpio_reset_pin(10);
        gpio_set_direction(10, GPIO_MODE_OUTPUT);
        gpio_reset_pin(7);
        gpio_set_direction(7, GPIO_MODE_OUTPUT);
        gpio_reset_pin(11);
        gpio_set_direction(11, GPIO_MODE_OUTPUT);
}

void app_main(void)
{
        struct st7789 lcd;

        board_init();

        /* set lcd param */
        lcd.param.fix.width = 240;
        lcd.param.fix.height = 240;
        lcd.param.fix.xoffset = 40;
        lcd.param.fix.yoffset = 40;
        lcd.param.var.orien = 0;

        /* register the operation */
        lcd.ops.pin.clk = esp32c3_clk_pin;
        lcd.ops.pin.mo = esp32c3_mo_pin;
        lcd.ops.pin.rst = esp32c3_rst_pin;
        lcd.ops.pin.cs = esp32c3_cs_pin;
        lcd.ops.pin.bl = esp32c3_bl_pin;
        lcd.ops.time.msleep = esp32c3_msleep;
        st7789_init(&lcd);

        while (1) {
                /* red */
                st7789_screen_fill(&lcd, RGB565_RED);

                /* blue */
                st7789_screen_fill(&lcd, RGB565_BLUE);

                /* black */
                st7789_screen_fill(&lcd, RGB565_BLACK);
        }
}
